#!/bin/sh
#
# lldpad       Start/Stop the LLDP agent.
#
# chkconfig: 2345 20 80
# description: Link Layer Discovery Protocol Agent Daemon

### BEGIN INIT INFO
# Provides: lldpad
# Required-Start: network
# Required-Stop:
# Default-Start:  2 3 4 5
# Default-Stop:
# Short-Description: Link Layer Discovery Protocol Agent Daemon
# Description: Link Layer Discovery Protocol Agent Daemon
### END INIT INFO

RETVAL=0
prog="lldpad"
exec=/usr/sbin/${prog}
lockfile=/var/lock/subsys/${prog}

# Source function library.
. /etc/rc.d/init.d/functions

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    daemon $prog -d
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
}

stop() {
    echo -n $"Stopping $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec
        RETVAL=3
    else
        failure $"Stopping $prog"
    fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}

restart() {
    rh_status_q && stop
    start
}

reload() {
    restart
}

force_reload() {
    # new configuration takes effect after restart
    restart
}

check_priv() {
    if [ $(id -u) -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p /var/run/${prog}.pid $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

check_priv || exit $?

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
